package cn.lanqiao.bigDate.web.controller;

import cn.lanqiao.bigDate.entity.sys.SysRole;
import cn.lanqiao.bigDate.entity.sys.SysUser;
import cn.lanqiao.bigDate.reslut.Result;
import cn.lanqiao.bigDate.web.BaseController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequestMapping(SysUserController.BASE_URL)
public class SysUserController extends BaseController {

    public static final String BASE_URL = "sysUser";

    /**
     * 测试接口
     * @param str   测试传入的参数
     * @return      访问成功返回的数据
     */
    @RequestMapping("/test")
    public String s(String str){
        System.out.println("\n<==== package: cn.lanqiao.bigDate.web.controller ====>");
        System.out.println(str);
        System.out.println("<==== author: Marisa ====>\n");
        return "success,param: " + str;
    }

    /**
     * 页面跳转
     * @return  页面mav
     */
    @RequestMapping(value = "toIndex")
    public ModelAndView toIndex() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName(BASE_URL + "/index"); //指定路径
        return mav;
    }

    /**
     * 分页查询
     * @param str   搜索的关键字
     * @return      分页结果
     */
    @GetMapping(value = "findSysUserPage")
    public IPage<SysUser> findByPage(Page<SysUser> page,String str) {
        return sysUserService.findByPage(page, str);
    }

    /**
     * 按ID查询用户
     * @param id    用户ID
     * @return  结果
     */
    @GetMapping(value = "findSysUserOne")
    public SysUser findSysUserOne(Long id) {
        return sysUserService.findSysUserOne(id);
    }

    /**
     * 修改用户信息
     * @param sysUser  修改后的用户
     * @return         结果
     */
    @PostMapping(value = "updateSysUser")
    public Result<String> updateSysUser(SysUser sysUser) {
        Result<String> result = new Result<>();
        Boolean isSuccess = sysUserService.updateSysUser(sysUser);
        Boolean isUpdate = sysUserService.saveOrUpdateRoleBatchBySysUser(sysUser);
        result.setCode(isSuccess && isUpdate ? 200 : 500);
        result.setMsg (isSuccess && isUpdate ? "修改成功" : "修改失败");
        return result;
    }

    /**
     * 保存用户信息
     * @param sysUser   用户
     * @return          结果
     */
    @PostMapping(value = "saveSysUser")
    public Result<String> saveSysUser(SysUser sysUser) {
        Result<String> result = new Result<>();
        Boolean isSuccess = sysUserService.saveSysUser(sysUser);
        Boolean isSaveRole = sysUserService.saveRoleBySysUser(sysUser);
        result.setCode(isSuccess && isSaveRole ? 200 : 500);
        result.setMsg (isSuccess && isSaveRole ? "添加成功" : "修改失败");
        return result;
    }

    /**
     * 按id删除
     * @param ids   id字符串
     * @return      删除结果
     */
    @RequestMapping(value = "deleteSysUser")
    public Result<String> deleteSysUser(String ids) {
        Result<String> result = new Result<>();
        Boolean isSuccess = sysUserService.deleteSysUser(ids);
        result.setCode(isSuccess ? 200 : 500);
        result.setMsg(isSuccess ? "删除成功" : "删除失败");
        return result;
    }

    /**
     * 根据用户的ID查询角色信息
     * @param id    在线用户的ID
     * @return      结果
     */
    @RequestMapping("/getRoleById")
    public Result<List<SysRole>> getRoleBySysUserId(String id){
        List<SysRole> roleById = sysUserService.getRolesById(id);
        Result<List<SysRole>> result = new Result<>();
        result.setCode(200);
        result.setMsg("获取成功");
        result.setData(roleById);
        System.out.println(result);
        return result;
    }
}